<?php
/**
 * Db           系统核心-数据库操作类
 * @copyright   Copyright(c) 2024-2034
 * @author      tianjie <527707665@qq.com>
 * @link        http://www.tinphp.com/
 * @version     4.1
 */

namespace tian;

use ReflectionClass;

class Db
{
    /**
     * 数据库初始化配置
     */
    public function __construct()
    {
        $database = config('database');
        if ($database['is_open']) {
            if (strtoupper($database['type']) == strtoupper('ThinkOrm')) {
                $configDb = [
                    'default' => $database['default'],
                    'connections' => [
                        $database['default'] => $database['connections'][$database['default']]
                    ],
                ];

                if (class_exists("\\think\\facade\\Db")) {
//                    $ThinkDb = new \think\facade\Db();
//                    $ThinkDb::setConfig($configDb);

                    $class = new ReflectionClass('\\think\\facade\\Db');
                    $instance = $class->newInstance();
                    $instance::setConfig($configDb);
                } else {
                    exit('请先 composer require topthink/think-orm 安装orm');
                }
            } else if (strtoupper($database['type']) == strtoupper('Medoo')) {
                //controller 中 请使用 use tian\Medoo\MedooDb;
                if (!class_exists("\\Medoo\\Medoo")) {
                    exit('请先 composer require catfan/medoo 安装orm');
                }
            } else {
                //其他模板引擎
                exit("暂时只支持 ThinkOrm 和 Medoo 引擎。");
            }
        }
    }

}